/**
 * 
 */
package com.py.framework.service.impl;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.py.framework.core.exception.ServiceException;
import com.py.framework.dao.SysElementDao;
import com.py.framework.entity.SysElement;
import com.py.framework.service.SysElementService;

/**
 * @author dd
 *
 */
@Service
public class SysElementServiceImpl implements SysElementService {

	@Autowired
	private SysElementDao sysElementDao;

	@Override
	public void addSysElement(CommonsMultipartFile favicon, CommonsMultipartFile background,
			CommonsMultipartFile loginLogo, CommonsMultipartFile sysLogo, CommonsMultipartFile scalingSysLogo,
			CommonsMultipartFile loginTitle, String copyright, String productName, String webUrl) throws IOException {

		if (favicon == null && background == null && loginLogo == null && sysLogo == null && scalingSysLogo == null
				&& loginTitle == null && copyright == null && productName == null && webUrl == null) {

			return;

		}

		byte[] backgoundBytes = null;
		byte[] loginLogoBytes = null;
		byte[] scalingSysLogoBytes = null;
		byte[] sysLogoBytes = null;
		byte[] loginTitleBytes = null;
		byte[] faviconBytes = null;

		if (favicon != null) {

			faviconBytes = favicon.getBytes();

		}

		if (loginTitle != null) {

			// 首页文字圖片：图片不超过1M，尺寸不超过1920*1080Px；

			BufferedImage image = ImageIO.read(loginTitle.getInputStream());

			if (image != null) {

				int width = image.getWidth();

				int height = image.getHeight();

				if (width > 1920 || height > 1080) {

					throw new ServiceException("首页文字圖片图尺寸不超过1920*1080Px");

				}

				loginTitleBytes = loginTitle.getBytes();

			} else {

				throw new ServiceException("首页文字上传文件非图片,请重新上传");
			}

			if (!"image/png".equals(loginTitle.getContentType()) && !"image/jpeg".equals(loginTitle.getContentType())
					&& !"image/jpg".equals(loginTitle.getContentType())) {

				throw new ServiceException("首页文字图必须是JPG、JEPG、PNG 格式");
			}

			if (loginTitle.getSize() > 1024 * 1024) {

				throw new ServiceException("首页文字图大小不能超过1M");

			}

		}

		if (background != null) {

			// 首页风格：图片不超过1M，尺寸不超过1920*1080Px；

			BufferedImage image = ImageIO.read(background.getInputStream());

			if (image != null) {

				int width = image.getWidth();

				int height = image.getHeight();

				if (width > 1920 || height > 1080) {

					throw new ServiceException("首页风格图尺寸不超过1920*1080Px");

				}

				backgoundBytes = background.getBytes();

			} else {

				throw new ServiceException("首页风格上传文件非图片,请重新上传");
			}

			if (!"image/png".equals(background.getContentType()) && !"image/jpeg".equals(background.getContentType())
					&& !"image/jpg".equals(background.getContentType())) {

				throw new ServiceException("首页风格图必须是JPG、JEPG、PNG 格式");
			}

			if (background.getSize() > 1024 * 1024) {

				throw new ServiceException("首页风格图大小不能超过1M");

			}

		}

		if (loginLogo != null) {

			// 登录首页logo：图片不超过300K，高度尺寸不超过70Px；

			BufferedImage image = ImageIO.read(loginLogo.getInputStream());

			if (image != null) {

				int height = image.getHeight();

				if (height > 70) {

					throw new ServiceException("首页logo高度尺寸不超过70Px");

				}

				loginLogoBytes = loginLogo.getBytes();

			} else {

				throw new ServiceException("首页logo上传文件非图片,请重新上传");
			}

			if (!"image/png".equals(loginLogo.getContentType()) && !"image/jpeg".equals(loginLogo.getContentType())
					&& !"image/jpg".equals(loginLogo.getContentType())) {

				throw new ServiceException("首页logo必须是JPG、JEPG、PNG 格式");
			}

			if (loginLogo.getSize() > 1024 * 300) {

				throw new ServiceException("首页logo大小不能超过300K");

			}

		}

		if (sysLogo != null) {

			// 后台界面logo：图片不超过300K，尺寸不超过220*49px；

			BufferedImage image = ImageIO.read(sysLogo.getInputStream());

			if (image != null) {

				int width = image.getWidth();

				int height = image.getHeight();

				if (width > 220 || height > 49) {

					throw new ServiceException("后台界面logo尺寸不超过220*49px");

				}

				sysLogoBytes = sysLogo.getBytes();

			} else {

				throw new ServiceException("后台界面logo上传文件非图片,请重新上传");
			}

			if (!"image/png".equals(sysLogo.getContentType()) && !"image/jpeg".equals(sysLogo.getContentType())
					&& !"image/jpg".equals(sysLogo.getContentType())) {

				throw new ServiceException("后台界面logo必须是JPG、JEPG、PNG 格式");
			}

			if (sysLogo.getSize() > 1024 * 300) {

				throw new ServiceException("后台界面logo大小不能超过300K");

			}

		}

		if (scalingSysLogo != null) {

			// 后台缩放logo：图片不超过300K，尺寸不超过60*49Px

			BufferedImage image = ImageIO.read(scalingSysLogo.getInputStream());

			if (image != null) {

				int width = image.getWidth();

				int height = image.getHeight();

				if (width > 60 || height > 49) {

					throw new ServiceException("后台缩放logo尺寸不超过60*49px");

				}

				scalingSysLogoBytes = scalingSysLogo.getBytes();

			} else {

				throw new ServiceException("后台缩放logo上传文件非图片,请重新上传");
			}

			if (!"image/png".equals(scalingSysLogo.getContentType())
					&& !"image/jpeg".equals(scalingSysLogo.getContentType())
					&& !"image/jpg".equals(scalingSysLogo.getContentType())) {

				throw new ServiceException("后台缩放logo必须是JPG、JEPG、PNG 格式");
			}

			if (scalingSysLogo.getSize() > 1024 * 300) {

				throw new ServiceException("后台缩放logo不能超过300K");

			}

		}

		SysElement record = new SysElement(faviconBytes, backgoundBytes, loginLogoBytes, sysLogoBytes,
				scalingSysLogoBytes, loginTitleBytes, copyright, productName, webUrl);
		SysElement selectLatest = sysElementDao.selectLatest();

		if (selectLatest != null) {

			record.setId(selectLatest.getId());

			sysElementDao.updateByPrimaryKeySelective(record);

		} else {

			sysElementDao.insertSelective(record);
		}

	}

	@Override
	public SysElement getLatestRecord() {

		SysElement latest = sysElementDao.selectLatest();
		return latest;
	}

}
